<?php if ( ! defined('BASEPATH')) exit('No direct script access allowed');

class Notification extends CI_Controller {
    
    var $Array_Data;
    
    function __construct() {
        parent::__construct();
        $UserInfo = $this->session->userdata('UserInfo');
        if($UserInfo['UserType'] != 'hire'){ $this->load->view('index.html'); return;}
    }
    
    public function get_notification(){
        $UserInfo = $this->session->userdata('UserInfo');
        $Notification_Info = array();
        $offset = 0;
        
        $CI =& get_instance();
        $CI->load->model('Users2_Notification_model','Us2Noti');
        $Notification_Info = $CI->Us2Noti->Get_Advanced("*",
                array('RelUserEmail'    =>  $UserInfo['UserEmail']),
                'AppearTime desc',
                10,0
                );
        //Arrange Data for View
        $this->Array_Data['UserInfo'] = $UserInfo;
        $this->Array_Data['Notification_Info']= $Notification_Info;

        $this->load->view('user_group2/notification_view',$this->Array_Data);

        
    }
    
    public function num_new_not(){
        $UserInfo = $this->session->userdata('UserInfo');
        $CI =& get_instance();
        $CI->load->model('Users2_Notification_model','Us2Noti');
        $NumNot = $CI->Us2Noti->Get(array(
            'IsReaded'          => 0,
            'RelUserEmail'      => $UserInfo['UserEmail']));
        echo count($NumNot);
    }
    
    public function show_notification($ID){
        $AdditionalData = $_GET['Additional'];
        $CI =& get_instance();
        $CI->load->model('Users2_Notification_model', 'Noti');
        $Noti =  $CI->Noti->Get(array('NotiID' => $ID));
        $Noti = $Noti[0];
        if($Noti['Type'] == 'Apply')
        {
            $RelApplicant = urldecode($AdditionalData[0]);
            $CI->load->model('Post_Applies_model', 'PA');
            $PA = $CI->PA->Get(array('RelPostID'    =>  $Noti['RelPostID'], 'RelUserEmailGroup3'    =>  $RelApplicant));
            $PA = $PA[0];
            
            $CI->load->model('Users3_model', 'Us3');
            $Us3 = $CI->Us3->Get(array('RelUserEmail' => $PA['RelUserEmailGroup3']));
            $Us3 = $Us3[0];
            $Link = site_url().'/user_group2/posts/'.$PA['RelPostID'].'?section=post-applicants'.'&NotiUsID='.$Us3['UserGroup3ID'];
            
            $CI->Noti->Update(array('IsReaded' =>  1),array('NotiID' => $ID));
            
            redirect($Link);
        }
        elseif($Noti['Type'] == 'App_Rev'){
            $Link = site_url().'/user_group2/posts/'.$Noti['RelPostID'].'?section=post-applicants';
            redirect($Link);
        }
    }
}